<template>
  <div class="login-container">
    <div class="login-box">
      <h2>旅游景点管理系统</h2>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        label-width="0"
      >
        <el-form-item prop="username">
          <el-input 
            v-model="loginForm.username"
            placeholder="请输入用户名"
          >
            <template #prefix>
              <el-icon><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            show-password
            @keyup.enter="handleLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button 
            type="primary" 
            :loading="loading"
            class="login-button"
            @click="handleLogin"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'
import api from '@/api/api'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)

const loginForm = ref({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  if (!loginFormRef.value) return
  
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    const res = await api.post('/api/admin/user/login', loginForm.value)
    
    // 如果后端返回了 data，表示登录成功，否则根据 msg 提示错误
    if (res.data) {
      console.log('登录成功，用户数据:', res.data)
      
      // 保存用户信息
      localStorage.setItem('userInfo', JSON.stringify(res.data))
      // 这里假设你的 token 是使用 id 拼接的
      localStorage.setItem('token', 'Bearer ' + res.data.id)

      ElMessage.success('登录成功')
      
      // 检查角色并跳转
      if (res.data.role === 1) {
        // 角色=1 => ScenicSpotList
        router.push('/ScenicSpotList')
      } else if (res.data.role === 3) {
        // 角色=2 => vendor
        router.push('/vendor')
      } else if (res.data.role === 2) {
        // 角色=3 => ScenicSpot
        router.push('/ScenicSpot')
      } else {
        // 其他情况，根据需求自行处理
        ElMessage.error('角色异常，登录失败')
      }
    } else {
      // 登录失败的情况
      ElMessage.error(res.msg || '登录失败')
    }
  } catch (error) {
    console.error('登录失败:', error)
    ElMessage.error(error.response?.data?.msg || '登录失败，请稍后重试')
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #1e90ff 0%, #70a1ff 100%);
}

.login-box {
  width: 400px;
  padding: 40px;
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #303133;
}

.el-input {
  --el-input-height: 40px;
}

.login-button {
  width: 100%;
  height: 40px;
}

:deep(.el-input__wrapper) {
  padding-left: 5px;
}

:deep(.el-input__prefix) {
  margin-left: 5px;
  color: #909399;
}

@media screen and (max-width: 576px) {
  .login-box {
    width: 90%;
    padding: 20px;
  }
}
</style>
